<?php
 defined("BASEDIR") or die();
 
 class Application_Controller_User extends Application_Controller_Default
 {
     
     #public $x;
	public function defaultAction()
	{
		$user = Application_Router_Standard_User::getInstance($this->request);
		if($user->level < $this->modInfo['requiredLevel'])
		{
			$this->request->action = "notallowed";
			return;
		}
		else 
                {
			$this->request->action = $this->request->controller;
                        $x=$this->request->x;
                        $this->view->assign("x", $x);
                        #print($x);
                        if ($x == 4) {
                            

/*
#print($username);
                            #print_r($qres);
#print($username);           

                            #$ip = (empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
                            $db->getTable("Zapisy");
                            $db->setFields("zid");

                          #  $db->where("pesel='".$username."'");
                            $num = $db->execute();
                            $qres = $db->getResult();
                            $zid=$qres["zid"];
                            #print($zid);

                            #$db = Application_Model_DataBase::getInstance();
                            $db->getTable("Zabiegi");
                            #$db->where("zid='".$zid."'");
                            $num1 = $db->execute();
                            $qres1 = $db->getResult();
                            $start =$qres1["start"];
                            $stop =$qres1["stop"];
                            $koniec=$qres1["koniec"];
                            $data=$qres1["data"];
                            $pid=$qres1["pid"];
                            $tid=$qres1["tid"];
                            $db->getTable("Typy_zabiegow");
                            $db->where("tid='".$tid."'");
                            $num2 = $db->execute();
                            $qres2 = $db->getResult();
                            $typ=$qres2["typ"];
                            #print($typ);
                            #print($start);
                            #print($stop);
                            #print($data);
                            $this->view->assign("typ", $typ);
                            $this->view->assign("start", $start);
                            $this->view->assign("stop", $stop);
                            $this->view->assign("data", $data);
                            
                    
  */              
                        }
                        elseif($x == 3) {
                            $db = Application_Model_DataBase::getInstance();
                            $ses = Application_Model_SessionControl::getInstance();
                            $username=$ses->un;
                           # print($username);
                            $db->getTable("Pacjenci");
                            $db->where("pesel='".$username."'");
                            $num = $db->execute();
                            $qres = $db->getResult();
                            #print_r($qres);
                            $pesel = $qres["pesel"];
                            $imie = $qres["imie"];
                            $nazwisko = $qres["nazwisko"];
                            $email = $qres["email"];
                            $telefon = $qres["telefon"];
                            $this->view->assign("pesel", $pesel);
                            $this->view->assign("imie", $imie);
                            $this->view->assign("nazwisko", $nazwisko);
                            $this->view->assign("email", $email);
                            $this->view->assign("telefon", $telefon);
                        }
                        
                }
        }
              
	
	protected function logoutAction()
	{
				
		$ses = Application_Model_SessionControl::getInstance();
		$ses->destroy();
		ob_end_clean();

		header("Location: ".constant("BASEDIR")."index.php");
		

		exit();
	}
	protected function rejestracjaAction()
	{
		$user = Application_Router_Standard_User::getInstance($this->request);
		if($user->level > 0)
			$this->request->action = "notallowed";
		else
		{
                        $this->view->assign("regstart", true);
			$zgoda = file_get_contents('./zgoda.txt');
			
			$this->view->assign('rule_txt', $zgoda);
			
			$this->modInfo['title'] = "Załóż konto";
			// weryfikacja danych
			$itemarr = array();
			$err = array();
			if($this->request->reg_pesel != "" && strlen($this->request->reg_pesel) == 11 && preg_match("/[0-9]/",$this->request->reg_pesel)) $itemarr['reg_pesel'] = $this->request->reg_pesel;
			else $err[] = "PESEL musi się składać z 11 znaków liczbowych";
			if($this->request->reg_email != "" && preg_match("/[a-zA-Z0-9_\.]@[0-9a-zA-Z_]{1,}.[a-z]/", $this->request->reg_email)) $itemarr['reg_email'] = $this->request->reg_email;
			else $err[] = "Adres email niepoprawny";
			if($this->request->reg_password1 != "" && $this->request->reg_password2 != "" && $this->request->reg_password1 == $this->request->reg_password2) $itemarr[] = $this->request->reg_password1;
			else $err[] = "Podane hasła nie zgadzają się";
			if(strlen($this->request->reg_password1) < 7) $err[] = "Hasło musi mieć co najmniej 8 znaków";
			if($this->request->reg_first_name != "") $itemarr['reg_first_name'] = $this->request->reg_first_name;
			else $err[] = "Nie podano imienia";
			if($this->request->reg_last_name != "") $itemarr['reg_last_name'] = $this->request->reg_last_name;
			else $err[] = "Nie podano nazwiska";
			if($this->request->reg_tel != "") $itemarr['reg_tel'] = $this->request->reg_tel;
			else $err[] = "Nie podano numeru telefonu";
			
			if($this->request->reg_accept == "on") $itemarr[] = $this->request_reg_accept;
			else $err[] = "Postanowienia regulaminu nie zostały zaakceptowane";
			
			if(empty($err) && !empty($itemarr))
			{
				
				$db = Application_Model_DataBase::getInstance();
				$ip = (empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
				
				$db->getTable("Pacjenci", "INSERT");
				$db->setFields("pesel, imie, nazwisko, email, telefon, haslo");
				$values = array($itemarr['reg_pesel'], $itemarr['reg_first_name'], $itemarr['reg_last_name'],   $this->request->reg_email, $itemarr['reg_tel'], md5($this->request->reg_password1));
				$db->setValues(implode(",", $values));
				$num = $db->execute();
				
				if($db->errno == 1062) // duplicate entry
				{          
					$this->view->assign("warning", "Podany PESEL lub e-mail jest już przypisany do innego konta");
					$this->view->assign("data", $itemarr);
				}
				if($num == 1)
				{         
					$this->view->assign("complete", $num);
					
				}
			}
			elseif(!empty($itemarr))
			{
                            ;
				$this->view->assign("errors", $err);
				$this->view->assign("data", $itemarr);
			}
		}
	}
        protected function wizytaAction() {
            
            $user = Application_Router_Standard_User::getInstance($this->request);
		if($user->level > 1)
			$this->request->action = "notallowed";
        }
        protected function reglogAction() {
            
            $user = Application_Router_Standard_User::getInstance($this->request);
		if($user->level > 0)
			$this->request->action = "notallowed";
                else
                    $this->view->assign("reglognext", true);
        }/*
        protected function ustaloneAction() {
            
            $user = Application_Router_Standard_User::getInstance($this->request);
            $username;
		if($user->level < 1)
			$this->request->action = "notallowed";
                else
                {$db = Application_Model_DataBase::getInstance();
                    $ses = Application_Model_SessionControl::getInstance();
                    $username=$ses->un;
                    print($username);
                    #$ip = (empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
                    $db->getTable("Zapisy");
                    $db->setFields("zid");
                    
                    $db->where("pesel='".$username."'");
                    $num = $db->execute();
                    $qres = $db->getResult();
                    $zid=$qres["zid"];
                    print($zid);
                    
                    #$db = Application_Model_DataBase::getInstance();
                    $db->getTable("Zabiegi");
                    $db->where("zid='".$zid."'");
                    $num1 = $db->execute();
                    $qres1 = $db->getResult();
                    $start =$qres1["start"];
                    $stop =$qres1["stop"];
                    $koniec=$qres1["koniec"];
                    $data=$qres1["data"];
                    $pid=$qres1["pid"];
                    $tid=$qres1["tid"];
                    $db->getTable("Typy_zabiegow");
                    $db->where("tid='".$tid."'");
                    $num2 = $db->execute();
                    $qres2 = $db->getResult();
                    $typ=$qres2["typ"];
                    $this->view->assign("typ", $typ);
                    $this->view->assign("start", $start);
                    $this->view->assign("stop", $stop);
                    $this->view->assign("data", $data);
                    
                }   
        }*/
 }
?>